import React, { useState, useEffect, useRef, useLayoutEffect } from 'react'; // 这行必须写，不然报错，可能是eslint搞事情。
import {
  Row,
  Input,
  Form,
  DatePicker,
  Modal,
  message,
  TreeSelect,
  Radio,
  Select,
  InputNumber,
  Tree,
} from 'antd';
const { confirm } = Modal;
const { RangePicker } = DatePicker;
const { TreeNode } = TreeSelect;
const { Option } = Select;
const { TextArea } = Input;
import { getRole, dataScope } from '@/api/system/role.js';
import { roleDeptTreeselect, deeps } from '@/api/system/dept.js';
const EditRoleDeptModalForm = ({
  visible,
  onhideRoleModal,
  onCancel,
  oninitList,
  roleId,
}) => {
  const [form] = Form.useForm();
  useResetFormOnCloseModal({
    form,
    visible,
  });

  const [treeData, settreeData] = useState([]);
  const [checkedKeys, setCheckedKeys] = useState([]);
  const [initValues, setinitValues] = useState(null);

  useEffect(() => {
    getRoles();
    getroleDeptTreeselect();
  }, [roleId]);

  //点击添加用户
  const onOk = () => {
    form.submit();
  };

  const onConcle = () => {
    console.log(onCancel, visible);
  };

  //表单提交方法
  const onFinish = values => {
    //菜单选择赋值
    values.roleId = roleId;
    values.deptIds = checkedKeys;

    dataScope(values).then(res => {
      if (res.code == 200) {
        message.success(res.msg);
        onhideRoleModal();
        oninitList();
      } else if (res.code == 500) {
        message.warning(res.msg);
      }
    });
  };
  //角色节点树选择
  const onCheck = checkedKeys => {
    setCheckedKeys(checkedKeys);
  };

  //获取角色详细信息
  const getRoles = () => {
    getRole(roleId).then(res => {
      if (res.code == 200) {
        setinitValues(res.data);
      }
    });
  };
  //获取角色部门下拉树
  const getroleDeptTreeselect = () => {
    roleDeptTreeselect(roleId).then(res => {
      if (res.code == 200) {
        settreeData(deeps(res.depts));
        setCheckedKeys(res.checkedKeys);
      }
    })
  }

  const layout = {
    labelCol: { span: 6 },
    wrapperCol: { span: 16 },
  };

  return (
    <Modal
      title="修改角色权限"
      visible={visible}
      onOk={onOk}
      onCancel={onCancel}
      cancelText="取消"
      okText="确定"
      style={{
        width: '300px !important',
        padding: '30px 20px',
        color: '#606266',
        fontSize: '14px',
        wordBreak: 'break-all',
      }}
    >
      {initValues !== null && (
        <Form
          {...layout}
          form={form}
          layout="horizontal"
          labelAlign="right"
          onFinish={onFinish}
          initialValues={initValues}
        >
          <Form.Item name="roleName" label="角色名称">
            <Input disabled />
          </Form.Item>
          <Form.Item name="roleKey" label="权限字符">
            <Input disabled />
          </Form.Item>
          <Form.Item name="dataScope" label="权限范围">
            <Select>
              <Option value="1">全部数据权限</Option>
              <Option value="2">自定义数据权限</Option>
              <Option value="3">本部门数据权限</Option>
              <Option value="4">本部门及以下数据权限</Option>
              <Option value="5">仅本人数据权限</Option>
            </Select>
          </Form.Item>
          <Form.Item label="数据权限">
            {treeData.length && (
              <Tree
                treeData={treeData}
                checkable
                onCheck={onCheck}
                checkedKeys={checkedKeys}
              ></Tree>
            )}
          </Form.Item>
        </Form>
      )}
    </Modal>
  );
};
// reset form fields when modal is form, closed
const useResetFormOnCloseModal = ({ form, visible }) => {
  const prevVisibleRef = useRef();
  useEffect(() => {
    prevVisibleRef.current = visible;
  }, [visible]);
  const prevVisible = prevVisibleRef.current;
  useEffect(() => {
    if (!visible && prevVisible) {
      form.resetFields();
    }
  }, [visible]);
};

export default EditRoleDeptModalForm;
